package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("套餐相关接口")
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @CacheEvict(cacheNames = "setmealCache" ,key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping()
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("setmealDTO:{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    /**
     * 如果是get请求依然可以用封装的形式接收数据，只不过不用加@RequestBody
     * Post请求必须加这个注解
     */
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult= setmealService.PageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation("根据id批量删除套餐")
    @DeleteMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("ids:{}", ids);
        setmealService.deleteById(ids);
        return Result.success();
    }
    @ApiOperation("修改套餐")
    @PutMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("setmealDTO:{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }
    @ApiOperation("根据id查询套餐数据")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmeal(@PathVariable Long id) {
        log.info("id:{}", id);
        SetmealVO setmealvo= setmealService.getById(id);
        return Result.success(setmealvo);
    }

    @ApiOperation("起售或停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStopSetmeal(@PathVariable Integer status,Long id) {
        setmealService.startOrStop(status,id);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    public Result<List<Setmeal>> list(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);
        List<Setmeal> list = setmealService.list(setmeal);
        return Result.success(list);
    }
}